import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:sports_new/entity/home/task/task_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/widgets/loading/no_data_widget.dart';

import '../../../server/api/king_api.dart';
import '../../../widgets/base/qkd_text.dart';
import '../../../widgets/loading/loading_widget.dart';

class PointHistoryPage extends StatefulWidget {
  const PointHistoryPage({super.key});

  @override
  State<PointHistoryPage> createState() => _PointHistoryPageState();
}

class _PointHistoryPageState extends State<PointHistoryPage> {
  List<PointsHistory>? entity;
  int _page = 1;
  EasyRefreshController controller = EasyRefreshController(controlFinishLoad: true);

  int get page => _page;
  set page(int value) {
    setState(() {
      _page = value;
    });
  }

  @override
  void initState() {
    refresh();
    super.initState();
  }

  Future refresh() async{
    page = 1;
    entity = await KingApi.getPointsHistory(page, 15);
    controller.resetFooter();
    setState(() {});
  }

  Future loadMore() async{
    page += 1;
    entity?.addAll(await KingApi.getPointsHistory(page, 15));
    controller.finishLoad(
      entity!.length < page*15?IndicatorResult.noMore:IndicatorResult.success);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Styles.appBar(
          backgroundColor: Colours.transparent, title: QkdText('积分明细')),
      backgroundColor: Colours.scaffoldBg,
      body: EasyRefresh.builder(
        controller: controller,
        footer: Styles.refreshFooter(position: IndicatorPosition.locator),
        onRefresh: refresh,
        onLoad: loadMore,
        childBuilder: (context,physics) => LoadingWidget<bool>(
          isLoading: entity == null,
          data: entity?.length == 0,
          noData: NoDataWidget(needScroll: true,physics: physics),
          child: () => ListView(
            physics: physics,
            children: List.generate(
              entity?.length ?? 0,
              (index) => cell(entity![index])
            ).append(const FooterLocator()),
          ),
        )
      ),
    );
  }

  Widget cell(PointsHistory data) {
    return Styles.card(
        padding: const EdgeInsets.all(12),
        margin: const EdgeInsets.fromLTRB(8, 10, 8, 0),
        child: Row(
          children: [
            Image.asset(
              Utils.getImgPath('task_point_${data.sign ?? 1}.png'),
              width: 40,
            ),
            const SizedBox(width: 12),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                QkdText(
                  data.message ?? "",
                  fontSize: 15,
                  fontWeight: FontWeight.w500,
                ),
                QkdText(
                  data.createTime ?? "",
                  color: Colours.grey99,
                  fontSize: 12,
                )
              ],
            ),
            const Spacer(),
            QkdText(
              '${data.sign == 1?"+":"-"}${data.points ?? 0}积分',
              color: data.sign == 1?Colours.main:Colours.blue2766D6,
              fontSize: 15,
            )
          ],
        ));
  }
}
